<?php

namespace app\model\user;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-UserCoupon
 * Class UserCoupon
 * @package app\model
 */
class UserCouponModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_coupon';

    /**
     * 范围查询-status
     * @param $query
     * @param $use
     */
    public function scopeUse($query, $use)
    {
        if (!empty($use)) {
            $query->where('use', '=', $use);
        }
    }

    /**
     * 范围查询-status
     * @param $query
     * @param $user_id
     */
    public function scopeUserid($query, $user_id = '')
    {
        if (!empty($user_id)) {
            $query->where('user_id', '=', $user_id);
        }
    }

    /**
     * 获取分页列表
     * @param $use
     * @param $user_id
     * @param $current_page
     * @param $list_rows
     * @param string $order
     * @return array
     */
    public function getPageData($use, $user_id = '', $current_page, $list_rows, $order = "id desc")
    {

        $dataList = $this
            ->field('*')
            ->use($use)
            ->userid($user_id)
            ->order($order)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 获取可用的兑换券列表
     * @param string $user_id
     * @param float $price
     * @return array|\think\Collection|static[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCouponList($user_id = '', $price = 0.00)
    {
        $map = [
            'user_id' => $user_id,
            'use' => 1
        ];
        $list = self::where($map)->where('price', "<=", $price)->field('id,price,type,detail')->select();
        if (empty($list)) {
            return [];
        }
        return $list;
    }

    /**
     * 获得优惠券
     * @param $user_id
     * @param $price
     * @param int $type 1商品兑换券
     * @param string $detail
     * @param string $remark
     * @return bool
     */
    public static function addCoupon($user_id, $price, $type = 1, $detail = "", $remark = "")
    {
        $in_data = [
            'user_id' => $user_id,
            'detail' => $detail,
            'type' => $type,
            'price' => $price,
            'use' => 1,
            'remark' => $remark
        ];
        $ret = self::insert($in_data);
        if ($ret != false) {
            return true;
        }
        return false;
    }

    /**
     * 使用优惠券
     * @param $user_id
     * @param $id
     * @return bool
     */
    public static function useCoupon($user_id, $id)
    {
        $map = [
            'user_id' => $user_id,
            'id' => $id
        ];
        $up_data = [
            'use' => 2,
            'use_time' => time()
        ];
        $ret = self::where($map)->update($up_data);
        if ($ret != false) {
            return true;
        }
        return false;
    }

    /**
     * 取消优惠券使用（返还优惠券）
     * @param $user_id
     * @param $id
     * @return bool
     */
    public static function cancelCoupon($user_id, $id)
    {

        $map = [
            'user_id' => $user_id,
            'id' => $id
        ];
        $up_data = [
            'use' => 1,
            'use_time' => 0
        ];
        $ret = self::where($map)->update($up_data);
        if ($ret != false) {
            return true;
        }
        return false;
    }

    /**
     * 优惠券过期
     * @param $user_id
     * @param $id
     * @return bool
     */
    public static function expiresCoupon($user_id, $id)
    {
        $map = [
            'user_id' => $user_id,
            'id' => $id
        ];
        $up_data = [
            'use' => 3,
            'use_time' => 0
        ];
        $ret = self::where($map)->update($up_data);
        if ($ret != false) {
            return true;
        }
        return false;
    }

    /**
     * 获取优惠券价格
     * @param $user_id
     * @param $id
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCouponPrice($user_id, $id)
    {
        $map = [
            'user_id' => $user_id,
            'id' => $id,
            'use' => 1
        ];
        $data_temp = self::where($map)->find();
        if (empty($data_temp)) {
            return 0;
        }
        return $data_temp['price'];
    }
}
